package com.tz.liveplayermodule;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.RequiresApi;
import android.view.Window;
import android.view.WindowManager;

import com.tz.liveplayermodule.jcvideoplayer.JCVideoPlayer;
import com.tz.liveplayermodule.util.CommonUtils;


public class PlayVideoActivity extends Activity {

    private JCVideoPlayer videoController;
    private PowerManager.WakeLock wakeLock;

    @SuppressLint("ResourceType")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            //如果系统回收的Activity， 但是系统却保留了Fragment， 当Activity被重新初始化， 此时， 系统保存的Fragment 的getActivity为空，
            //所以要移除旧的Fragment ， 重新初始化新的Fragment
            String FRAGMENTS_TAG = "android:support:fragments";
            savedInstanceState.remove(FRAGMENTS_TAG);
        }
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_play_video);


        CommonUtils.keepScreenOn(wakeLock, this, true);
        CommonUtils.keepScreenLongLight(this, true);

        System.out.println("url:" + getIntent().getStringExtra("filePath"));
        System.out.println("title:" + getIntent().getStringExtra("fileName"));
        videoController = (JCVideoPlayer) findViewById(R.id.videocontroller);
        videoController.setUpForFullscreen(getIntent().getStringExtra("filePath"), "", getIntent().getStringExtra("fileName"));
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoController.quitFullScreen();
            return;
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        JCVideoPlayer.releaseAllVideos();
        CommonUtils.keepScreenOn(wakeLock, this, false);
        CommonUtils.keepScreenLongLight(this, false);
    }
}
